<template>
  <el-container class="common-layout">
    <el-aside width="200px" class="aside-menu">
      <el-menu
        default-active="/"
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        router
      >
        <h3 class="logo-title">后台管理</h3>
        <el-menu-item index="/">
          <el-icon><House /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="/users">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/inquiry">
          <el-icon><ChatDotRound /></el-icon>
          <span>问诊记录</span>
        </el-menu-item>
        <el-menu-item index="/healthcheck">
          <el-icon><Monitor /></el-icon>
          <span>健康检测</span>
        </el-menu-item>
        <el-menu-item index="/chronic">
          <el-icon><Warning /></el-icon>
          <span>慢病管理</span>
        </el-menu-item>
        <el-menu-item index="/diet">
          <el-icon><KnifeFork /></el-icon>
          <span>饮食记录</span>
        </el-menu-item>
        <el-menu-item index="/exercise">
          <el-icon><Basketball /></el-icon>
          <span>运动记录</span>
        </el-menu-item>
        <el-menu-item index="/lecture">
          <el-icon><Reading /></el-icon>
          <span>健康讲座</span>
        </el-menu-item>
        <el-menu-item index="/registration">
          <el-icon><Tickets /></el-icon>
          <span>讲座报名</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header-bar">
        <el-button @click="toggleCollapse">
          <el-icon><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
        </el-button>
        <div class="user-info">
          <span>{{ username }}</span>
          <el-button type="danger" size="small" @click="logout">退出登录</el-button>
        </div>
      </el-header>
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'
import {
  House,
  User,
  ChatDotRound,
  Monitor,
  Warning,
  KnifeFork,
  Basketball,
  Reading,
  Tickets,
  Expand,
  Fold
} from '@element-plus/icons-vue'

const isCollapse = ref(false)
const username = ref('Admin') // 示例用户名
const router = useRouter()
const userStore = useUserStore()

onMounted(() => {
  // 在这里获取实际的用户名
  // username.value = userStore.userInfo.username || 'Admin'
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const logout = () => {
  userStore.clearToken()
  router.push('/login')
}
</script>

<style scoped>
.common-layout {
  height: 100vh;
}

.aside-menu {
  background-color: #2b2b2b;
  color: #fff;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  border-right: none;
  background-color: #2b2b2b;
}

.el-menu-item {
  color: #fff;
}

.el-menu-item.is-active {
  background-color: #409eff !important;
  color: #fff !important;
}

.el-menu-item:hover {
  background-color: #3a3a3a !important;
}

.header-bar {
  background-color: #fff;
  color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.logo-title {
  color: #fff;
  text-align: center;
  height: 60px;
  line-height: 60px;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-info span {
  margin-right: 15px;
  font-size: 16px;
}
</style>